﻿using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using KPIS.GERP.Models;
using KPIS.GERP.Models.SYS;

namespace KPIS.GERP.WCF.SYS
{
    [ServiceContract]
    public interface ISysMessageService
    {
        [OperationContract]
        int GetSendUnwarnCount(int empId);

        [OperationContract]
        int GetSendUnreadCount(int empId);

        [OperationContract]
        int GetSendUnwarnUnreadCount(int empId, out int unreadCount);

        [OperationContract]
        MessageSend GetSendInfo(int sendId);

        [OperationContract]
        List<MessageSend> GetSendsInfo(int empId);

        [OperationContract]
        void MarkSendAsWarn(int sendId);

        [OperationContract]
        void MarkSendAsRead(int sendId);

        [OperationContract]
        void MarkAllSendOfEmployeeAsWarn(int empId);

    }

    public class SysMessageService : ISysMessageService
    {
        // Remark:
        // Message Status = 0 is Unread
        // Message Status = 1 is Read
        // Message Status = 2 is Replied
        #region Implementation of IMessageService

        public int GetSendUnwarnCount(int empId)
        {
            int unwarnCount = 0;
            using (var db = new GERPContext(true))
            {
                var messages = db.MessageSends.Where(o =>
                                                     o.RecordStatus != RecordStatus.DeletedStatus &&
                                                     o.ToEmpId == empId &&
                                                     !o.IsWarn);
                foreach (var m in messages)
                { 
                    m.IsWarn = true;
                    unwarnCount++;
                }
                db.SaveChanges();
            }
            return unwarnCount;
        }

        public int GetSendUnreadCount(int empId)
        {
            int unreadCount = 0;
            using (var db = new GERPContext(true))
            {
                // 0=Unread
                // 1=Read
                // 2=Replied
                var messages = db.MessageSends.Where(o =>
                                                     o.RecordStatus != RecordStatus.DeletedStatus &&
                                                     o.ToEmpId == empId &&
                                                     (o.MessageStatus == "0" || !o.IsWarn));
                foreach(var m in messages)
                {
                    if(!m.IsWarn) // mark incorrect iswarn flag as warn
                    {
                        m.IsWarn = true;
                    }
                    if(m.MessageStatus == "0")
                    {
                        unreadCount++;
                    }
                }
                db.SaveChanges();
            }
            return unreadCount;
        }

        public int GetSendUnwarnUnreadCount(int empId, out int unreadCount )
        {
            int unwarnCount = 0;
            unreadCount = 0;
            using (var db = new GERPContext(true))
            {
                var messages = db.MessageSends.Where(o => o.ToEmpId == empId ); 
                messages = messages.Where(o => o.RecordStatus != RecordStatus.DeletedStatus ); 
                foreach (var m in messages)
                {
                    bool isWarn = m.IsWarn;
                    if(!isWarn) // Unwarn
                    {
                        m.IsWarn = true;
                        unwarnCount++;
                    }
                    if(m.MessageStatus == "0") // Unread
                    {
                        unreadCount++;
                    }
                }
                db.SaveChanges();
            }
            return unwarnCount;
        }

        public MessageSend GetSendInfo(int sendId)
        {
            using (var db = new GERPContext(true))
            {
                return db.MessageSends.FirstOrDefault(o =>
                                                      o.SendId == sendId &&
                                                      o.RecordStatus != RecordStatus.DeletedStatus);
            }
        }

        public List<MessageSend> GetSendsInfo(int empId)
        {
            using (var db = new GERPContext(true))
            {
                return db.MessageSends.Where(o =>
                                             o.ToEmpId == empId &&
                                             o.RecordStatus != RecordStatus.DeletedStatus)
                                             .ToList();
            }
        }

        public void MarkSendAsWarn(int sendId)
        {
            using (var db = new GERPContext(true))
            {
                var ms = db.MessageSends.FirstOrDefault(o =>
                                                        o.SendId == sendId && 
                                                        o.RecordStatus != RecordStatus.DeletedStatus);
                if (ms != null && !ms.IsWarn)
                {
                    ms.IsWarn = true;
                    db.SaveChanges();
                }
            }
        }

        public void MarkSendAsRead(int sendId)
        {
            using (var db = new GERPContext(true))
            {
                var ms = db.MessageSends.FirstOrDefault(o =>
                                                        o.SendId == sendId &&
                                                        o.RecordStatus != RecordStatus.DeletedStatus);
                
                if (ms != null && ms.MessageStatus != "2")
                {
                    ms.MessageStatus = "2";
                    db.SaveChanges();
                }
            }
        }

        public void MarkAllSendOfEmployeeAsWarn(int empId)
        {
            using (var db = new GERPContext(true))
            {
                var ms = db.MessageSends.Where(o =>
                                               o.ToEmpId == empId &&
                                               o.RecordStatus != RecordStatus.DeletedStatus);
                foreach (var m in ms)
                {
                    m.IsWarn = true;
                }
                db.SaveChanges();
            }
        }

        #endregion
    }
}